package org.galaxyworld.flycode.gui;

import org.galaxyworld.flycode.config.ConfigManager;
import org.galaxyworld.flycode.config.ServiceConfigData;
import org.galaxyworld.flycode.events.MainWindowEvent;
import org.galaxyworld.flycode.events.MainWindowEventListener;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class ServiceConfigPane extends AbstractConfigPane implements MainWindowEventListener {

    private JCheckBox enableBox;
    private JTextField packageField;
    private JLabel packageLabel;
    private JLabel templateNameLabel;
    private JTextField templateField;
    private JLabel implPackageLabel;
    private JTextField implPackageField;

    private boolean shown = false;

    public ServiceConfigPane() {
        super("Service");

        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                shown = true;
            }

            @Override
            public void componentHidden(ComponentEvent e) {
                ServiceConfigData serviceConfigData = ConfigManager.getConfig(ServiceConfigData.class);
                serviceConfigData.setEnabled(enableBox.isSelected());
                if (shown && enableBox.isSelected()) {
                    serviceConfigData.setPackageName(packageField.getText());
                    serviceConfigData.setImplPackageName(implPackageField.getText());
                    serviceConfigData.setTemplateName(templateField.getText());
                }
            }
        });
    }

    @Override
    protected void setupMainPane(JPanel mainPane) {
        GroupLayout layout = new GroupLayout(mainPane);
        mainPane.setLayout(layout);

        JLabel enableLabel = new JLabel("Enable");
        enableBox = new JCheckBox();
        templateNameLabel = new JLabel("Template");
        templateField = new JTextField();
        packageLabel = new JLabel("Interface Package");
        packageField = new JTextField();
        implPackageLabel = new JLabel("Implementation Package");
        implPackageField = new JTextField();

        layout.setHorizontalGroup(
                layout.createSequentialGroup()
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(enableLabel)
                                .addComponent(templateNameLabel)
                                .addComponent(packageLabel)
                                .addComponent(implPackageLabel))
                        .addGap(MARGIN)
                        .addGroup(layout.createParallelGroup()
                                .addComponent(enableBox)
                                .addComponent(templateField)
                                .addComponent(packageField)
                                .addComponent(implPackageField))
                        .addGap(GAP));
        layout.setVerticalGroup(
                layout.createSequentialGroup()
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(enableLabel)
                                .addComponent(enableBox))
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(templateNameLabel)
                                .addComponent(templateField))
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(packageLabel)
                                .addComponent(packageField))
                        .addGap(GAP)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(implPackageLabel)
                                .addComponent(implPackageField))
                        .addGap(GAP));

        enableBox.setSelected(true);
        enableBox.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                boolean enabled = enableBox.isSelected();
                setComponentEnabled(enabled);
            }
        });
    }

    private void setComponentEnabled(boolean enabled) {
        templateNameLabel.setEnabled(enabled);
        templateField.setEnabled(enabled);
        packageLabel.setEnabled(enabled);
        packageField.setEnabled(enabled);
        implPackageLabel.setEnabled(enabled);
        implPackageField.setEnabled(enabled);
    }

    @Override
    public void windowOpened(MainWindowEvent e) {
        ServiceConfigData serviceConfigData = ConfigManager.getConfig(ServiceConfigData.class);
        packageField.setText(serviceConfigData.getPackageName());
        implPackageField.setText(serviceConfigData.getImplPackageName());
        templateField.setText(serviceConfigData.getTemplateName());
    }

    @Override
    public void windowClosing(MainWindowEvent e) {

    }

}
